<template>
  <div>
  <el-dialog :title="(type == 'detail')?'调拨单详情':'调拨单收货'" :close-on-click-modal="false" :visible.sync="visible" fullscreen center append-to-body>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-row v-if="type == 'detail'">
      <el-link  style="margin-right:15px;" :href="printUrl" target="_blank" type="info"><el-button type="info" @click="getPrintParams">打印单据(品牌)</el-button></el-link>
      <el-link  style="margin-right:15px;"  :href="printUrl" target="_blank" type="info"><el-button type="info" @click="getGoodsPrintParams">打印单据(商品)</el-button></el-link>
      <el-link  style="margin-right:15px;"  :href="printUrl" target="_blank" type="info"><el-button type="info" @click="getSalePrintParams">打印销售出库单(含批次)</el-button></el-link>
      <el-link  style="margin-right:15px;"  :href="printUrl" target="_blank" type="info"><el-button type="info" @click="getSalePrintParams2">打印销售出库单</el-button></el-link>
      <el-link  style="margin-right:15px;"  :href="printUrl" target="_blank" type="info"><el-button type="info" @click="getAllocationInfo">打印退货单(含批次)</el-button></el-link>
      <el-link  style="margin-right:15px;"  :href="printUrl" target="_blank" type="info"><el-button type="info" @click="getAllocation">打印退货单</el-button></el-link>
      <el-link  style="margin-right:15px;" :href="printUrl" target="_blank" type="info"><el-button type="info" @click="getSaleInPrintParams">打印销售入库单</el-button></el-link>
      <el-link  style="margin-right:15px;" :href="printUrl" target="_blank" type="info"><el-button type="info" @click="getSaleInPrintParams2">打印销售入库单(含批次)</el-button></el-link>
    </el-row>
    <el-divider content-position="left">单据信息</el-divider>
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" label-width="90px">
      <el-row>
        <el-col>
          <el-form-item label="调拨单号">
            <el-input disabled v-model="dataForm.billId"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="发出部门" prop="outDeptId">
            <el-select v-model="dataForm.outDeptId" @change="getOutStockList" disabled>
              <el-option v-for="item in deptOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="发出仓库" prop="outStockId">
            <el-select v-model="dataForm.outStockId"  disabled>
              <el-option v-for="item in outStockOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="接收部门" prop="inDeptId">
            <el-select v-model="dataForm.inDeptId"  disabled @change="getInStockList">
              <el-option v-for="item in deptOptions" :key="item.id" :label="item.name" :value="item.id" :disabled="item.id == dataForm.outDeptId"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="接收仓库" prop="inStockId">
            <el-select v-model="dataForm.inStockId"  disabled>
              <el-option v-for="item in inStockOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="制单人">
            <el-input disabled v-model="dataForm.createUserName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="制单日期">
            <el-input disabled v-model="dataForm.createTime"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="审核人">
            <el-input disabled v-model="dataForm.auditUserName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="审核时间">
            <el-input disabled v-model="dataForm.auditTime"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="收货人">
            <el-input disabled v-model="dataForm.receiveUserName"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="收货时间">
            <el-input disabled v-model="dataForm.receiveTime"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-form-item label="备注">
          <el-input disabled type="textarea" v-model="dataForm.memo"></el-input>
        </el-form-item>
      </el-row>

    </el-form>
    <el-form :inline="true">
      <el-form-item><el-button type="info" @click="printBarcode">打印条码</el-button></el-form-item>
      <div class="upload-wrap">
        <input ref="configInput" class="file-ele" id="config" type="file" @change="showConfig(this)"></input>
        <div class="file-open">打印配置</div>
      </div>
    </el-form>
    <el-divider content-position="left">商品信息</el-divider>
    <el-table :data="dataList" border v-loading="dataListLoading" show-summary :summary-method="getSummaries" @selection-change="handleSelectionChange">
      <el-table-column key="1" type="selection" width="55"></el-table-column>
      <el-table-column prop="goodsCode" label="商品代码" width="140"></el-table-column>
      <el-table-column prop="goodsName" label="商品名称" min-width="230"></el-table-column>
      <el-table-column prop="spec" label="型号" width="100"></el-table-column>
      <el-table-column prop="price" label="零售价格" width="100"></el-table-column>
      <el-table-column prop="storageQuantity" label="可调用数量" width="80"></el-table-column>
      <el-table-column prop="allocationQuantity" label="调拨数量" width="80"></el-table-column>
      <el-table-column prop="barcode" label="商品条码" width="140"></el-table-column>
      <el-table-column prop="invalidTime" label="效期" width="140"></el-table-column>
      <el-table-column prop="batch" label="批次" width="100"></el-table-column>
      <el-table-column prop="registerNum" label="注册证号" width="140"></el-table-column>
      <el-table-column prop="manufacturerName" label="生产厂家" width="100"></el-table-column>
    </el-table>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" v-if="type == 'receive'" @click="dataFormSubmit" :loading="ischeck" v-prevent-re-click>收 货</el-button>
    </span>
  </el-dialog>

  </div>
</template>

<script>
  import {mapGetters} from 'vuex'
  import {allocationGetObj,receiveAllocation} from '@/api/eims/allocation'

  import {getDeptInfo} from '@/api/admin/dept'
  import {getStockList} from '@/api/eims/stock'
  import { PrintAllocationBarcode } from '@/util/printDs'

  let params = '/allocationBrandPrint.cpt&allocationId='
  let goodsParams = '/allocationGoodsPrint.cpt&allocationId='
  let saleParams = 'InOutBound/OutboundDelivery.cpt&allocationId='
  let saleParams2 = 'InOutBound/OutboundDeliveryInfo.cpt&allocationId='
  let saleInParams = 'InOutBound/InboundDeliveryInfo.cpt&allocationId='
  let saleInParams2 = 'InOutBound/InboundDelivery.cpt&allocationId='
  let alloParamInfo = 'InOutBound/allocationReturnInfo.cpt&allocationId='
  let alloParam = 'InOutBound/allocationReturn.cpt&allocationId='

  export default {
    data () {
      return {
        type: '',
        visible: false,
        ischeck: false,
        singleListVisible: false,
        detailVisible: false,
        dataListLoading: false,
        printUrl: '',
        dataForm: {
          billId: '',   // 调拨单号
          linkBillId: '', //   关联单号
          outDeptId: '',  // 发出部门
          outDeptName: '',
          outStockId: '', // 发出仓库
          inDeptId: '', //  接收部门
          inDeptName: '',
          inStockId: '',  //  接收仓库
          createUser: 1, //  制单人
          detailList: []
        },
        dataList: [],
        deptOptions: [],
        outStockOptions: [],
        inStockOptions: [],
        selectList: [],
        dataRule: {
          billId: [
            { required: true, message: '调拨单号不能为空'}
          ]

        }
      }
    },
    computed: {
      ...mapGetters(['userInfo','bizData'])
    },
    methods: {
      init (id,type) {
        this.dataForm.detailList = []
        this.dataList = []
        this.dataForm.id = id || 0
        this.type = type
        this.visible = true

        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          this.getDeptList()
          this.dataForm.outDeptId = this.userInfo.lessDeptId
          this.dataForm.outDeptName = this.userInfo.lessDeptName
          this.getOutStockList (this.userInfo.lessDeptId)
          if (this.dataForm.id) {
            allocationGetObj(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
                this.getInStockList(response.data.data.inDeptId)
                this.getOutStockList(response.data.data.outDeptId)
                this.dataForm = response.data.data
                this.dataList = response.data.data.detailList
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      // 获取部门列表
      getDeptList () {
        getDeptInfo().then(response => {
          if(response.data.code == '0'){
            this.deptOptions = response.data.data
          }else{
            this.$message.error(response.data.msg)
          }
        })
      },
      // 获取出库仓位列表
      getOutStockList (id) {
        //  获取发出部门名称
        this.deptOptions.forEach(item => {
          if(item.id == id){
            this.dataForm.outDeptName = item.name
          }
        })
        this.dataForm.outStockId = ''
        if(id){
          getStockList({deptId: id}).then(response => {
            if(response.data.code == '0'){
              this.outStockOptions = response.data.data
            }else{
              this.$message.error(response.data.msg)
            }
          })
        }else{
          this.$message.warning('请选择发出部门')
        }
      },
      // 获取入库仓位列表
      getInStockList (id) {
        //  获取接收部门名称
        this.deptOptions.forEach(item => {
          if(item.id == id){
            this.dataForm.inDeptName = item.name
          }
        })
        this.dataForm.inStockId = ''
        if(id){
          getStockList({deptId: id}).then(response => {
            if(response.data.code == '0'){
              this.inStockOptions = response.data.data
            }else{
              this.$message.error(response.data.msg)
            }
          })
        }else{
          this.$message.warning('请选择接收部门')
        }
      },
      handleSelectionChange(val) {
        this.selectList = val
      },
			// 配置打印设置
			showConfig(that){
			  var _that = this
			  var fr=new FileReader();
			  var cover=document.getElementById('config').files[0];
			  fr.readAsText(cover);
			  fr.onload=function(e){
			    localStorage.setItem('printData', '')
			    localStorage.setItem('printData', this.result)
			  }
			},
      // 打印条码
      printBarcode(){
        let that = this
        if(this.selectList.length < 1){
          alert("请钩选要打印的商品条码！");
          return false
        }
        let printData = localStorage.getItem('printData')
        if(printData){
          this.printConfig = printData
          this.$confirm('条码打印确认！','提示',{confirmButtonText:'确定',cancelButtonText:'取消',type: 'warning'})
        			.then(() => {
        				// web_DSPrtEngineStartup1()
        				PrintAllocationBarcode(that)
        			})
        }else{
          alert('请配置选择打印配置文件')
        }
      },
      //   获取品牌打印的参数
      getPrintParams(){
        let allocationId = this.dataForm.id
        this.printUrl = this.bizData.printUrl + params + allocationId
      },
      //   获取打印的参数
      getGoodsPrintParams(){
          let allocationId = this.dataForm.id
          this.printUrl = this.bizData.printUrl + goodsParams + allocationId
      },
        // 获取销售出库参数(含批次)
      getSalePrintParams(){
          let allocationId = this.dataForm.id
          this.printUrl = this.bizData.printUrl + saleParams + allocationId
      },
      // 获取销售出库参数
      getSalePrintParams2(){
          let allocationId = this.dataForm.id
          this.printUrl = this.bizData.printUrl + saleParams2 + allocationId
      },
        // 获取调拨退货单参数(含批次)
      getAllocationInfo(){
          let allocationId = this.dataForm.id
          this.printUrl = this.bizData.printUrl + alloParamInfo + allocationId
      },
      // 获取调拨退货单
      getAllocation(){
          let allocationId = this.dataForm.id
          this.printUrl = this.bizData.printUrl + alloParam + allocationId
      },
      // 获取销售入库参数
      getSaleInPrintParams(){
          let allocationId = this.dataForm.id
          this.printUrl = this.bizData.printUrl + saleInParams + allocationId
      },
      // 获取销售入库（含参数）参数
      getSaleInPrintParams2(){
          let allocationId = this.dataForm.id
          this.printUrl = this.bizData.printUrl + saleInParams2 + allocationId
      },
      // 表单提交
      dataFormSubmit () {
        this.$confirm('是否确认收货调拨单?', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).then(() => {
          this.ischeck = true
          return receiveAllocation({id: this.dataForm.id})
        }).then(response => {
          if(response.data.code == '0'){
            this.$message({ type: 'success',  message: '收货成功!' });
            this.visible = false
            this.$emit('refreshDataList')
          }else{
            this.$message.error(response.data.msg)
          }
          this.ischeck = false
        })
      },
      //  合计
      getSummaries(param) {
        const { columns, data } = param;
        const sums = [];
        columns.forEach((column, index) => {
          const values = data.map(item => Number(item[column.property]));
          if (!values.every(value => isNaN(value))) {
            if(index == 6){
              sums[index] = '合计： ' + values.reduce((prev, curr) => {
                const value = Number(curr);
                if (!isNaN(value)) {
                  return prev + curr;
                } else {
                  return prev;
                }
              }, 0)
            }
          }
        });
        return sums;
      },
    }
  }
</script>
<style lang="scss" scoped>
  /deep/ .el-table {
    display: flex;
    flex-direction: column;
  }
  /deep/ .el-table__body-wrapper {
    order: 1;
  }
  .upload-wrap{
    position: relative;
    float: right;
    display: inline-block;
    overflow: hidden;
    border-radius: 3px;
    margin-left: 10px;
  }
  .upload-wrap .file-ele{
    position: absolute;
    top:0;
    right:0;
    opacity: 0;
    height: 100%;
    width: 100%;
    cursor: pointer;
  }
  .upload-wrap .file-open{
    width:90px;
    height:30px;
    line-height:30px;
    text-align: center;
    color:#666;
    background-color: #fff;
  }
</style>
